//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.cmc.cloud.cmclink.doc.util.so;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.cmc.cloud.cmclink.common.filter.FilterGroup;
import com.cmc.cloud.cmclink.common.filter.FilterInfo;
import com.cmc.cloud.cmclink.common.filter.FilterRule;
import com.cmc.cloud.cmclink.common.filter.enums.FilterCombineType;
import com.cmc.cloud.cmclink.common.filter.exceptions.FilterBuildException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotNull;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

/**
 * PDF生成工具类
 * @author chenzheng
 * @date 2025/07/31 11:25
 */

public class FilterExprBuilderNew {
    private static final Logger log = LoggerFactory.getLogger(FilterExprBuilderNew.class);
    protected static final String AND = " && ";
    protected static final String OR = " || ";
    public static final String LEFT_BRACKET = "(";
    public static final String RIGHT_BRACKET = ")";

    private FilterExprBuilderNew() {
    }

    public static String build(FilterInfo filterInfo) {
        return build(filterInfo, new Function<String, String>() {
            public String apply(String s) {
                return s;
            }
        });
    }


    public static String build(FilterInfo filterInfo, Function<String, String> fieldNameConverter) {
        if (null != filterInfo && null != filterInfo.getFilterGroup()) {
            return !hasFilter(filterInfo.getFilterGroup()) ? null : buildNew(filterInfo.getFilterGroup(), fieldNameConverter);
        } else {
            return null;
        }
    }

    private static String build(FilterGroup filterGroup, Function<String, String> fieldNameConverter) {
        StringBuilder builder = new StringBuilder();
        //
        if (CollectionUtil.isNotEmpty(filterGroup.getFilterRules())) {
            builder.append(CollectionUtil.isNotEmpty(filterGroup.getFilterRules()) ? buildList(filterGroup.getFilterRules(), filterGroup.getFilterType(), fieldNameConverter) : "");
        }

        if (CollectionUtil.isNotEmpty(filterGroup.getFilterGroups())) {
            Iterator var3 = filterGroup.getFilterGroups().iterator();

            while (var3.hasNext()) {
                FilterGroup group = (FilterGroup) var3.next();
                if (hasFilter(group)) {
                    String expr = build(group, fieldNameConverter);
                    if (!StrUtil.isBlank(expr)) {
                        if (filterGroup.getFilterType() == FilterCombineType.AND) {
                            if (builder.length() == 0) {
                                builder.append("(").append(expr).append(")");
                            } else {
                                builder.append(" && ").append("(").append(expr).append(")");
                            }
                        } else if (builder.length() == 0) {
                            builder.append("(").append(expr).append(")");
                        } else {
                            builder.append(" || ").append("(").append(expr).append(")");
                        }
                    }
                }
            }
        }

        return builder.toString();
    }


    private static String buildNew(FilterGroup filterGroup, Function<String, String> fieldNameConverter) {
        String listStr = "";
        String groupStr = "";
        //转数组
        if (CollectionUtil.isNotEmpty(filterGroup.getFilterRules())) {
            listStr = buildList(filterGroup.getFilterRules(), filterGroup.getFilterType(), fieldNameConverter);
        }
        // // 递归子类  filterRules拼接
        if (CollectionUtil.isNotEmpty(filterGroup.getFilterGroups())) {
            Iterator var3 = filterGroup.getFilterGroups().iterator();
            while (var3.hasNext()) {
                FilterGroup group = (FilterGroup) var3.next();
                if (hasFilter(group)) {
                    String expr = build(group, fieldNameConverter);
                    if (!StrUtil.isBlank(expr)) {
                        groupStr += groupStr.length() == 0 ? expr : (filterGroup.getFilterType() == FilterCombineType.AND ? " && " : " || ") + expr;
                    }
                }
            }
            //拼装开头结尾
            if (!StrUtil.isBlank(groupStr)) {
                groupStr = "(" + groupStr + ")";
            }
        }
        if (!StrUtil.isBlank(listStr) && !StrUtil.isBlank(groupStr)) {
            return "(" + listStr + (filterGroup.getFilterType() == FilterCombineType.AND ? " && " : " || ") + groupStr + ")";
        } else if (!StrUtil.isBlank(listStr) || !StrUtil.isBlank(groupStr)) {
            return "(" + listStr + groupStr + ")";
        } else {
            return "";
        }
    }


    private static boolean hasFilter(FilterGroup group) {
        if (CollectionUtil.isNotEmpty(group.getFilterRules())) {
            return true;
        } else {
            if (CollectionUtil.isNotEmpty(group.getFilterGroups())) {
                Iterator var1 = group.getFilterGroups().iterator();

                while (var1.hasNext()) {
                    FilterGroup subGroup = (FilterGroup) var1.next();
                    if (hasFilter(subGroup)) {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    private static String buildList(@NotNull List<FilterRule> rules, FilterCombineType filterType, Function<String, String> fieldNameConverter) {
        StringBuilder conditionBuilder = new StringBuilder("");
        Iterator var5 = rules.iterator();
        while (var5.hasNext()) {
            FilterRule rule = (FilterRule) var5.next();
            if (null == rule.getFieldType()) {
                log.warn("field {} is not supported type", rule.getFieldName());
                throw new FilterBuildException(rule.getFieldName() + " is not supported type:" + rule.getFieldType());
            }
            if (null == rule.getCondition()) {
                log.warn("condition of field {} is not specified or not supported", rule.getFieldName());
            } else {
                String expr = rule.getCondition().getExprBuilder().build((String) fieldNameConverter.apply(rule.getFieldName()), rule.getValue(), rule.getFieldType());
                conditionBuilder.append(conditionBuilder.length() == 0 ? expr : (filterType == FilterCombineType.AND ? " && " : " || ") + expr);
            }
        }
        return "(" + conditionBuilder.toString() + ")";
    }
}
